package com.matt.service.impl;

import com.matt.domain.Contract;
import com.matt.domain.ContractHistory;
import com.matt.commons.dto.sso.StaffDTO;
import com.matt.foundation.utils.DateUtil;
import com.matt.repository.ContractHistoryRepository;
import com.matt.repository.ContractRepository;
import com.matt.service.ContractOperationLogService;
import com.matt.remoteCall.SsoCallService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;

import static com.matt.foundation.constant.OperateConstant.OPERATE_BREAK;
import static com.matt.foundation.constant.OperateConstant.OPERATE_TRANSFER;

/**
 * ************************
 * Created by Ron
 * from com.matt
 * on 2018/7/24 0024.
 * ************************
 */
@Service
public class ContractOperationLogServiceImpl implements ContractOperationLogService {

    @Autowired
    private ContractHistoryRepository contractHistoryRepository;
    @Autowired
    private ContractRepository contractRepository;
    @Autowired
    private SsoCallService ssoCallService;


    @Override
    public ContractHistory changeStaffInfo(StaffDTO operator, StaffDTO staff, String contractId) {

        Contract contract = contractRepository.getOne(contractId);
        ContractHistory contractHistory = new ContractHistory();
        contractHistory.setOperationType(OPERATE_TRANSFER);
        contractHistory.setContent( operator.getRealName() +  " 在 "+ DateUtil.dateToymd(new Date())+" 将本数据转移给了\"" + staff.getRealName()+"\"");
        contractHistory.setClienteleId(contract.getClienteleId());
        contractHistory.setStaffId(operator.getId());
        contractHistory.setStaffName(operator.getRealName());
        contractHistory.setStaffHeadImgUrl(operator.getHeadImgUrl());
        contractHistory.setContractId(contractId);
        return contractHistoryRepository.save(contractHistory);
    }

    @Override
    public ContractHistory breakDownContract(StaffDTO baseInfo, String contractId) {
//        StaffDTO baseInfo = ssoCallService.getStaffDTO(ticket);
        Contract contract = contractRepository.getOne(contractId);
        ContractHistory contractHistoryVo = new ContractHistory();
        contractHistoryVo.setOperationType(OPERATE_BREAK);
        contractHistoryVo.setContent("在 "+DateUtil.dateToymd(new Date())+ "中止了合同" );
        contractHistoryVo.setClienteleId(contract.getClienteleId());
        contractHistoryVo.setStaffId(baseInfo.getId());
        contractHistoryVo.setStaffName(baseInfo.getRealName());
        contractHistoryVo.setStaffHeadImgUrl(baseInfo.getHeadImgUrl());
        contractHistoryVo.setContractId(contractId);
        return contractHistoryRepository.save(contractHistoryVo);
    }


}
